import React, { useState } from 'react';
import { useEffect } from 'react';
import { Link, connect, history } from 'umi';
import styles from './style.less';
import { Button, Card, Input, Skeleton, Result, Modal } from 'antd';
import UserBrief from '@/components/UserBrief'
import { rejectInvite, acceptInvite, acceptLinkInvite ,getEnterpriseInviteDetail ,getInviteDetail} from '@/services/enterprise'


const Invite = (props) => {

    const {
        dispatch,
        rejectInviteLoading,
        isLogin,
        noticeId,
        location
    } = props


    const [inviteCode, setInviteCode] = useState();
    const [enterpriseId, setEnterpriseId] = useState();
    const [inviteDetail,setInviteDetail] = useState();
    const [errorResponse,setErrorResponse] = useState();
    const [acceptInviteLoading,setAcceptInviteLoading] = useState(false)

    const [rejectSuccess, setRejectSuccess] = useState(false);

    useEffect(() => {
        queryInviteData();
        setRejectSuccess(false)
    }, [location])

    useEffect(() => {
        queryInviteData()
    }, [])

    const queryInviteData = () => {
        let inviteCode = history.location.query.inviteCode
        let enterpriseId = history.location.query.enterpriseId
        setInviteCode(inviteCode);
        setEnterpriseId(enterpriseId);
        console.log(isLogin, inviteCode, enterpriseId)
        if (isLogin || localStorage.getItem('isLogin')) {
            if (history.location.pathname.indexOf("/home/invite") === -1) {
                if (enterpriseId) {
                    history.replace(`/home/invite?inviteCode=${inviteCode}`)
                }
                if (inviteCode) {
                    history.replace(`/home/invite?enterpriseId=${enterpriseId}`)
                }
            }
        }
        if (dispatch) {
            if (enterpriseId) {
                getEnterpriseInviteDetail(enterpriseId).then((result) => {
                    if (result && result.success) {
                        setInviteDetail(result.data)
                    } else {
                        setErrorResponse(result)
                    }
                })
            }
            if (inviteCode) {
                getInviteDetail(inviteCode).then((result) => {
                    if (result && result.success) {
                        setInviteDetail(result.data)
                    } else {
                        setErrorResponse(result)
                    }
                })
            }
        }
    }

    const doRejectInvite = () => {
        rejectInvite(enterpriseId).then(({ success }) => {
            if (success) {
                setRejectSuccess(true)
            }
        })
    }

    const doAcceptInvite = () => {
        let inviteCode = history.location.query.inviteCode;
        let enterpriseId = history.location.query.enterpriseId;
        setAcceptInviteLoading(true)
        if (enterpriseId) {
            acceptInvite(enterpriseId).then((success) => {
                setAcceptInviteLoading(false)
                if (success) {
                    Modal.success({
                        title: "接受邀请成功",
                        content: '您已接受邀请。',
                    });
                    history.replace("/")
                }
            })
        }
        if (inviteCode) {
            acceptLinkInvite(inviteCode).then((success) => {
                setAcceptInviteLoading(false)
                if (success) {
                    Modal.success({
                        title: "接受邀请成功",
                        content: '您已接受邀请，企业管理员确认您的信息后即可加入企业！',
                    });
                }
            })
        }

    }

    if (errorResponse) {
        return (<Result
            status="error"
            subTitle={inviteCode ? "邀请链接已经过期或者不存在!" : "您的邀请信息不存在！"}
        >
        </Result>)
    }

    if (rejectSuccess) {
        return (<Result
            status="info"
            subTitle={"您已拒绝邀请"}
        >
        </Result>)
    }

    if (!inviteDetail) {
        return (
            <Skeleton></Skeleton>
        )
    }

    return (
        <>
            <Card className={styles.main}>
                <div>
                    亲爱的 {inviteDetail.user ? (inviteDetail.user.nickName || inviteDetail.user.username) : "用户"} :
                    <div className={styles.user}>
                        <UserBrief user={inviteDetail.invitorUser}>
                        </UserBrief>
                        <div> 邀请你加入</div>
                    </div>
                    <div className={styles.enterpriseName}>
                        {inviteDetail.enterpriseName}
                    </div>
                    <div className={styles.secondInfo}>
                        部门: {inviteDetail.enterpriseDepartmentName}
                    </div>
                    <div className={styles.secondInfo}>
                        角色: {inviteDetail.enterpriseRoleName}
                    </div>

                </div>
                <div className={styles.confirmEnter}>
                    <Button className={styles.action} type="primary" loading={acceptInviteLoading} onClick={doAcceptInvite}>确认加入</Button>
                    {enterpriseId &&
                        <Button danger className={styles.action} loading={rejectInviteLoading} onClick={doRejectInvite}>确认拒绝</Button>
                    }
                </div>
                {inviteDetail.expireIn && (
                    <div className={styles.expireIn}>
                        有效期至: {inviteDetail.expireIn}
                    </div>
                )}
            </Card>
        </>
    )
}

export default connect(({ oauth, loading }) => ({
    isLogin: oauth.isLogin,
}))(Invite);